{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tkinter\n",
    "import tkinter.messagebox\n",
    "import tkinter.ttk\n",
    "\n",
    "\n",
    "#创建tkinter应用程序\n",
    "root = tkinter.Tk()\n",
    "#设置窗口标题\n",
    "root.title('Selection widgets')\n",
    "#定义窗口大小\n",
    "root['height'] = 450\n",
    "root['width'] = 370\n",
    "\n",
    "\n",
    "#与姓名关联的变量\n",
    "varName = tkinter.StringVar()\n",
    "varName.set('')\n",
    "#创建标签然后放到窗口上\n",
    "labelName = tkinter.Label(root,\n",
    "                          text='姓名',\n",
    "                          justify=tkinter.RIGHT,\n",
    "                          width=50)\n",
    "labelName.place(x=30,y=5,width=50,height=20)\n",
    "#创建文本框，同时设置关联变量\n",
    "entryName = tkinter.Entry(root,\n",
    "                          width=120,\n",
    "                          textvariable=varName)\n",
    "entryName.place(x=70,y=5,width=120,height=20)\n",
    "\n",
    "labelGrade = tkinter.Label(root,\n",
    "                           text='年级',\n",
    "                           justify = tkinter.RIGHT,width=50)\n",
    "labelGrade.place(x=5,y=40,width=100,height=20)\n",
    "#模拟学生所在年级，字典键为年级，值为班级\n",
    "studentClasses = {'1年级':['1班','2班','3班','4班'],\n",
    "                  '2年级':['1班','2班'],\n",
    "                  '3年级':['1班','2班','3班']}\n",
    "#学生年级组合框\n",
    "comboGrade = tkinter.ttk.Combobox(root,width=50,\n",
    "                                  values=tuple(studentClasses.keys()))\n",
    "comboGrade.place(x=70,y=40,width=75,height=20)\n",
    "#事件处理函数\n",
    "def comboChange(event):\n",
    "    grade = comboGrade.get()\n",
    "    if grade:\n",
    "        #动态改变组合框可选项\n",
    "        comboClass[\"values\"] = studentClasses.get(grade)\n",
    "    else :\n",
    "        comboClass.set([])\n",
    "#绑定组合框事件处理函数\n",
    "comboGrade.bind('<<ComboboxSelected>>' , comboChange)\n",
    "\n",
    "\n",
    "labelClass = tkinter.Label(root,\n",
    "                           text='班级',\n",
    "                           justify=tkinter.RIGHT,width=50)\n",
    "labelClass.place(x=170,y=40,width=50,height=20)\n",
    "#学生班级组合框\n",
    "comboClass = tkinter.ttk.Combobox(root,width=50)\n",
    "comboClass.place(x=220,y=40,width=50,height=20)\n",
    "\n",
    "\n",
    "labelSex = tkinter.Label(root,\n",
    "                         text='性别',\n",
    "                         justify=tkinter.RIGHT,width=50)\n",
    "labelSex.place(x=10,y=70,width=100,height=20)\n",
    "#与性别关联变量，1为雄，2为雌，默认雄\n",
    "sex = tkinter.IntVar()\n",
    "sex.set(1)\n",
    "#单选钮，雄\n",
    "redionMan = tkinter.Radiobutton(root,\n",
    "                                variable=sex,\n",
    "                                value=1,\n",
    "                                text=\"男\")\n",
    "redionMan.place(x=70,y=70,width=50,height=20)\n",
    "#单选钮，雌\n",
    "redionWoman = tkinter.Radiobutton(root,\n",
    "                                  variable=sex,\n",
    "                                  value=0,\n",
    "                                  text=\"女\")\n",
    "redionWoman.place(x=130,y=70,width=70,height=20)\n",
    "\n",
    "#与是否班长关联变量，默认当前角色非班长\n",
    "monitor = tkinter.IntVar()\n",
    "monitor.set(0)\n",
    "#复选框，选中时变量值为1，未选中时值为0\n",
    "checkMonitor = tkinter.Checkbutton(root,\n",
    "                                   text='是班长？',\n",
    "                                   variable = monitor,\n",
    "                                   onvalue=1,\n",
    "                                   offvalue=0)\n",
    "checkMonitor.place(x=20,y=100,width=100,height=20)\n",
    "#添加按钮单击事件处理函数\n",
    "def addInformation() :\n",
    "    result = '名字' + entryName.get()\n",
    "    result = result + '年级' + comboGrade.get()\n",
    "    result = result + '班级' + comboClass.get()\n",
    "    result = result + '性别' \\\n",
    "             + ('男' if sex.get() else '女')\n",
    "    result = result + '班长' \\\n",
    "             + ('是' if monitor.get() else '否')\n",
    "    #把信息插入到列表框组件中\n",
    "    listboxStudents.insert(0,result)\n",
    "buttonAdd = tkinter.Button(root,\n",
    "                           text='添加内容',\n",
    "                           width=40,\n",
    "                           command = addInformation)\n",
    "buttonAdd.place(x=130,y=100,width=100,height=20)\n",
    "\n",
    "#删除按钮的处理事件\n",
    "def deleteSelection() :\n",
    "    selection = listboxStudents.curselection()\n",
    "    if not selection :\n",
    "        tkinter.messagebox.showinfo(title='信息',\n",
    "                                    message='背水一战，没有选择')\n",
    "    else :\n",
    "        listboxStudents.delete(selection)\n",
    "buttonDelete = tkinter.Button(root,\n",
    "                              text='删除内容',\n",
    "                              width=100,\n",
    "                              command=deleteSelection)    \n",
    "buttonDelete.place(x=230,y=100,width=100,height=20)\n",
    "\n",
    "\n",
    "#创建列表框组件\n",
    "listboxStudents = tkinter.Listbox(root,width=300)\n",
    "listboxStudents.place(x=10,y=130,width=300,height=200)\n",
    "\n",
    "\n",
    "#启动消息循环\n",
    "root.mainloop()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
